<template>
    <div class="channel" v-loading="loading">
        <div class="input-box">
            <el-input
                placeholder="请输入您要搜索的内容"
                clearable
                style="width:200px;"
                @keyup.enter.native="getDataList"
                v-model.trim="keyWord">
                <i slot="prefix" class="el-input__icon el-icon-search"></i>
            </el-input>
            <el-button type="primary" class="btn" @click="getDataList">搜索</el-button>
            <el-button type="success" class="btn" @click="goDetail(0)">新建</el-button>
        </div>
        <div class="table-box">
            <el-table
                :data="tableData"
                ref="multipleTable"
                @select-all="selectAll"
                border
                style="width: 100%"
                @selection-change="handleSelectionChange">
                <el-table-column
                    v-if="type"
                    type="selection"
                    :selectable="canSelect"
                    width="55">
                </el-table-column>
                <el-table-column
                    fixed
                    width="55"
                    prop="topicid"
                    label="id">
                </el-table-column>
                <el-table-column
                    width="180"
                    prop="title"
                    label="专题名">
                </el-table-column>
                <el-table-column
                    width="100"
                    prop="remark"
                    label="模板样式">
                    <template slot-scope="scope">
                        <span v-if="scope.row.template === 1">游戏列表</span>
                        <span v-else-if="scope.row.template === 2">banner系列</span>
                        <span v-else-if="scope.row.template === 4">游戏资讯</span>
                        <span v-else-if="scope.row.template === 5">游戏资讯</span>
                    </template>
                </el-table-column>
                <el-table-column
                    width="120"
                    label="关联频道">
                    <template slot-scope="scope">
                        <span>{{scope.row.channelLabel || '-'}}</span>
                    </template>
                </el-table-column>
                <!-- <el-table-column
                    prop="address"
                    label="展示样式">
                </el-table-column> -->
                <el-table-column
                    prop="status"
                    width="100"
                    label="排期">
                    <template slot-scope="scope">
                        <div style="text-align:center;">
                            <div v-if="scope.row.channelStartTime">{{scope.row.channelStartTime | timeNo0}}</div>
                            <div>-</div>
                            <div v-if="scope.row.channelEndTime">{{scope.row.channelEndTime | timeNo0}}</div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="状态">
                    <!-- -1下架0待上架1上架2暂停 -->
                    <template slot-scope="scope">
                        <div v-if="scope.row.channelStatus === -1" class="color:#909399;">已下架</div>
                        <div v-else-if="scope.row.channelStatus === 0" style="color: #E6A23C;">待上架</div>
                        <div v-else-if="scope.row.channelStatus === 1" style="color: #67C23A;">已上架</div>
                        <div v-else style="color: #303133;">暂停</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="creator"
                    label="创建人">
                    <template slot-scope="scope">
                        <div>{{scope.row.creator || '-'}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="110"
                    label="修改时间">
                    <template slot-scope="scope">
                        <div style="text-align:center">{{scope.row.updateTime || '-' | timeNo0}}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="110"
                    label="创建时间">
                    <template slot-scope="scope">
                        <div style="text-align:center">{{scope.row.creatTime || '-' | timeNo0}}</div>
                    </template>
                </el-table-column>
                <template v-if="!type">
                    <el-table-column
                        fixed="right"
                        label="操作">
                        <template slot-scope="scope">
                            <div><el-button type="text" size="small" @click="beforeDeleteItem(scope.row)" style="color: #F56C6C;">删除</el-button></div>
                            <div><el-button @click="goDetail(scope.row.topicid)" type="text" size="small">查看详情</el-button></div>
                        </template>
                    </el-table-column>
                </template>
            </el-table>
        </div>

        <div class="btn-box" v-if="type">
            <el-button @click="close">取消</el-button>
            <el-button @click="check" type="primary" class="check-btn">选中{{checkList.length}}条</el-button>
        </div>
        <div class="tools" v-else>
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <el-dialog
        title="提示"
        :visible.sync="dialogVisible">
        <span>此操作可能直接影响线上数据，删除后不可恢复，确定要删除？</span>
        <span slot="footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="deleteItem">确 定</el-button>
        </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data () {
        return {
            keyWord: '', // 查询关键字
            data: [], // 总数据
            total: 0, // 数据总条数
            pageNum: 1,
            pageSize: this._pageSize,
            loading: false,
            dialogVisible: false, // 切换状态弹窗
            checkList: [],
            disabledId: []
        }
    },
    props: {
        type: {},
        disabledList: {
            default: () => {
                return []
            }
        }
    },
    watch: {
        // 传过来的禁止列表 转换为禁止id列表
        disabledList (val) {
            this.disabledId = []
            val.forEach((item) => {
                this.disabledId.push(item.topicid)
            })
        }
    },
    computed: {
        tableData () {
            if (this.type) {
                return this.data
            } else {
                let start = this.pageNum * this.pageSize - this.pageSize
                let end = this.pageNum * this.pageSize
                return this.data.slice(start, end)
            }
        },
        checkidList () {
            let idList = []
            this.checkList.forEach((item) => {
                idList.push(item.topicid)
            })
            return idList
        }
    },
    created () {
        this.disabledList.forEach((item) => {
            this.disabledId.push(item.topicid)
        })
        this.getDataList()
    },
    activated () {
        if (this.$route.meta.keepAlive) {
            // 防止查询后输入keyword 导致返回时列表不一致
            if (this.listParams) {
                this.keyWord = this.listParams.keyword
            }
            this.getDataList()
        }
    },
    methods: {
        // 查看列表
        async getDataList () {
            if (this.loading) return
            this.loading = true
            this.listParams = {}
            if (this.keyWord) {
                this.listParams.keyword = this.keyWord
            }
            let res = await this.$http.post('/home/findTopics', this.listParams)
            if (res.code === '0') {
                this.data = res.data
                this.total = res.data.length
            } else {
                this.$message({
                    message: res.desc,
                    type: 'error'
                })
            }
            this.loading = false
        },

        // 页容切换
        sizeChange (size) {
            this.pageSize = size
            this.pageNum = 1
        },

        // 页码切换
        pageChange (num) {
            this.pageNum = num
        },

        // 查看详情
        goDetail (id) {
            let query = {}
            if (this.$route.path === '/index/channelDetail') {
                query.from = 'channelDetail'
            }
            if (id) {
                query.id = id
            }
            this.$router.push({
                path: '/index/projectDetail',
                query
            })
        },

        // 切换频道状态弹窗确认
        beforeDeleteItem (item, status) {
            this.currentItem = item
            this.dialogVisible = true
        },

        // 切换频道状态
        async deleteItem () {
            let params = {
                topicId: this.currentItem.topicid
            }
            let res = await this.$http.post('/home/deleteTopic', params)
            if (res.code === '0') {
                this.data.forEach((item, index) => {
                    if (item.topicid === this.currentItem.topicid) {
                        this.currentIndex = index
                    }
                })
                this.data.splice(this.currentIndex, 1)
            } else {
                this.$message({
                    type: 'error',
                    message: '修改失败，请稍后再试'
                })
            }
            this.dialogVisible = false
        },

        /**
         * 弹窗对应 api
         */

        // 关闭弹窗
        close () {
            this.$emit('close')
        },

        // 是否可以选中
        canSelect (row) {
            if (this.disabledId.includes(row.topicid)) {
                return false
            }
            if (this.type === 'one' && this.checkList.length >= 1 && !this.checkidList.includes(row.topicid)) {
                return false
            }
            return true
        },

        // 选中切换事件
        handleSelectionChange (val) {
            this.checkList = val
        },

        // 选中
        check () {
            if (this.checkList.length) {
                this.$emit('updateProject', this.checkList)
            }
            this.$refs.multipleTable.clearSelection()
            this.$emit('close')
        },

        // 全选事件
        selectAll () {
            if (this.type === 'one') {
                this.$refs.multipleTable.clearSelection()
            }
        }
    },
    beforeRouteLeave (to, from, next) {
        if (to.path === '/index/projectDetail') {
            from.meta.keepAlive = true
        } else {
            from.meta.keepAlive = false
        }
        next()
    }
}
</script>

<style lang="scss" scoped>
    .channel {
        .input-box {
            .btn {
                margin-left: 10px;
            }
        }
        .table-box {
            margin: 20px 0;
        }

        .btn-box {
            padding-right: 20px;
            margin-top: 20px;
            display: flex;
            justify-content: flex-end;
            .check-btn {
                margin-left: 50px;
            }
        }
        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }

        .channel-detail {
            background-color: #ffffff;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            z-index: 99;
        }
    }
</style>
